package com.link4a.doms.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.link4a.doms.dao.Page;
import com.link4a.doms.dao.handler.DriverHandler;
import com.link4a.doms.dao.handler.LicenseHandler;
import com.link4a.doms.exception.BusinessException;
import com.link4a.doms.exception.ValidationException;
import com.link4a.doms.model.Driver;
import com.link4a.doms.model.License;
import com.link4a.doms.vo.DriverCondition;

@Service
public class DriverService {
	
	@Autowired
	private DriverHandler driverHandler;
	@Autowired
	private LicenseHandler licenseHandler;

	public Driver findById(int id) {
		return driverHandler.findById(id);
	}
	
	public Page<Driver> findPage(DriverCondition driverCondition) {
		return driverHandler.findPage(driverCondition);
	}
	
	public void save(Driver driver) throws ValidationException {
		if (driverHandler.isNameDuplicated(driver)) {
			throw new ValidationException(ValidationException.DATA_DUPLICATED);
		}
		
		driverHandler.merge(driver);
	}
	
	public void delete(int id) throws BusinessException {
		List<License> licenses = licenseHandler.findByDriver(id);
		if (licenses != null && !licenses.isEmpty()) {
			throw new BusinessException("该驱动授权已售出，不允许删除");
		}
		driverHandler.delete(id);
	}
	
	public List<Driver> listByKeyword(String keyword) {
		return driverHandler.listByKeyword(keyword);
	}
}